package com.cxb;

import org.xutils.common.util.LogUtil;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

import com.cxb.manager.RootContext;
import com.cxb.utils.SharedPreferencesUtils;

public class BrightnessActivity extends BaseActivity {
	private SeekBar sbBrightness;
	private View rl_bg;
	private CheckBox cb_auto_light;
	private SharedPreferencesUtils spPreferencesUtils;
	private int minBrightness = 30;
	Toast taost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Settings.System.putInt(getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
		
		setContentView(R.layout.activity_setting_brightness);
		rl_bg = this.findViewById(R.id.rl_bg);
		rl_bg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				BrightnessActivity.this.finish();
			}
		});
		init();

		BroadcastReceiver mReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				if (intent.getAction().equals(
						"android.intent.action.setBrightness")) {
					if (sbBrightness == null)
						return;
					try {
						sbBrightness.setProgress(Settings.System.getInt(
								getContentResolver(),
								Settings.System.SCREEN_BRIGHTNESS)
								- minBrightness);
					} catch (SettingNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};

		IntentFilter intentFilter = new IntentFilter(
				"android.intent.action.setBrightness");
		registerReceiver(mReceiver, intentFilter);
		taost = Toast.makeText(this, "", Toast.LENGTH_SHORT);

	}

	private void init() {
		cb_auto_light = (CheckBox) findViewById(R.id.cb_auto_light);

		sbBrightness = (SeekBar) findViewById(R.id.sb_brightness);
		int maxb = 255 - minBrightness;

		sbBrightness.setMax(maxb);
		try {
			sbBrightness.setProgress(Settings.System.getInt(
					getContentResolver(), Settings.System.SCREEN_BRIGHTNESS)
					- minBrightness);
		} catch (SettingNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sbBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				rl_bg.setBackgroundColor(0x99000000);
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				setScreenBritness(progress + minBrightness);
				//
				LogUtil.i("onProgressChanged progress=" + progress
						+ " fromUser=" + fromUser);
				// rl_bg = findViewById(R.id.rl_bg);
				// rl_bg.setBackgroundColor(0x000000000);
				// // TODO Auto-generated method stub
				// // Settings.System.putInt(getContentResolver(),
				// // Settings.System.SCREEN_BRIGHTNESS_MODE,
				// // Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
				// Settings.System.putInt(getContentResolver(),
				// Settings.System.SCREEN_BRIGHTNESS, progress
				// + minBrightness);

				// spPreferencesUtils.setBrightness(progress + minBrightness);
				// taost.setText("B: "+(progress + minBrightness));
				// taost.show();

				// WindowManager.LayoutParams lp = BrightnessActivity.this
				// .getWindow().getAttributes();
				//
				// lp.screenBrightness = (progress + minBrightness) / 255.0f;
				// BrightnessActivity.this.getWindow().setAttributes(lp);

				if (fromUser)
					cb_auto_light.setChecked(false);
			}
		});

		spPreferencesUtils = new SharedPreferencesUtils(this);
		cb_auto_light.setChecked(spPreferencesUtils.isAutoLight());
		// sbBrightness.setEnabled(!spPreferencesUtils.isAutoLight());
		cb_auto_light.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				spPreferencesUtils.setIsAutoLight(isChecked);
				sbBrightness.setEnabled(!isChecked);

			}
		});
	}

	// 屏幕亮度
	public void setScreenBritness(int brightness) {
		// 不让屏幕全暗
		if (brightness <= 1) {
			brightness = 1;
		}
		// 设置当前activity的屏幕亮度
		WindowManager.LayoutParams lp = this.getWindow().getAttributes();
		// 0到1,调整亮度暗到全亮
		lp.screenBrightness = Float.valueOf(brightness / 255f);
		this.getWindow().setAttributes(lp);
		
		RootContext.getInstance().runCommand(
				"echo " + brightness
						+ " > /sys/class/leds/lcd-backlight/brightness");
		// 保存为系统亮度方法1
		android.provider.Settings.System.putInt(getContentResolver(),
				android.provider.Settings.System.SCREEN_BRIGHTNESS, brightness);

	}
}
